﻿using Zen4SyncModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

namespace Zen4SyncModel.Test
{


    /// <summary>
    ///Classe de test pour ILoggerTest, destinée à contenir tous
    ///les tests unitaires ILoggerTest
    ///</summary>
    [TestClass()]
    public class ILoggerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Obtient ou définit le contexte de test qui fournit
        ///des informations sur la série de tests active ainsi que ses fonctionnalités.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Attributs de tests supplémentaires
        // 
        //Vous pouvez utiliser les attributs supplémentaires suivants lorsque vous écrivez vos tests :
        //
        //Utilisez ClassInitialize pour exécuter du code avant d'exécuter le premier test dans la classe
        [ClassInitialize()]
        public static void MyClassInitialize( TestContext testContext )
        {
            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                z4sEntities.sp_z4s_purge_database( true );
                z4sEntities.sp_z4s_insert_reference_data();
                z4sEntities.sp_z4s_insert_test_data_ILogger();
            }
        }
        //
        //Utilisez ClassCleanup pour exécuter du code après que tous les tests ont été exécutés dans une classe
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Utilisez TestInitialize pour exécuter du code avant d'exécuter chaque test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Utilisez TestCleanup pour exécuter du code après que chaque test a été exécuté
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        internal virtual ILogger CreateILogger()
        {
            ILogger target = LoggerFactory.Instance;
            return target;
        }

        /// <summary>
        ///Test pour ClientSessionIsReadyToStart
        ///</summary>
        [TestMethod()]
        public void ClientSessionIsReadyToStartTest()
        {
            ILogger target = CreateILogger();
            
            Guid clientSessionId_1 = new Guid( "87843D09-C873-45DA-8E0E-B66A30D719A6" );
            int processId_1 = 0;
            DateTime dateTime_1 = DateTime.Now;
            target.ClientSessionIsReadyToStart( clientSessionId_1, processId_1, dateTime_1 );
            
            Guid clientSessionId_2 = new Guid( "468CF8B1-4B26-4BF7-B9B1-DA57249F9281" );
            int processId_2 = 1;
            DateTime dateTime_2 = DateTime.Now;
            target.ClientSessionIsReadyToStart( clientSessionId_2, processId_2, dateTime_2 );
            
            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                clientSession clientSession_1 = z4sEntities.clientSession.Where( cs1 => cs1.clientSession_id == clientSessionId_1 ).SingleOrDefault();
                Assert.IsNotNull( clientSession_1.clientSession_readyToStartDateTimeOnServer );

                clientSession clientSession_2 = z4sEntities.clientSession.Where( cs2 => cs2.clientSession_id == clientSessionId_2 ).SingleOrDefault();
                Assert.IsNotNull( clientSession_2.clientSession_readyToStartDateTimeOnServer );
            }
        }

        /// <summary>
        ///Test pour ClientSessionStarted
        ///</summary>
        [TestMethod()]
        public void ClientSessionStartedTest()
        {
            ILogger target = CreateILogger();

            Guid clientSessionId_1 = new Guid( "87843D09-C873-45DA-8E0E-B66A30D719A6" );
            DateTime dateTime_1 = DateTime.Now;
            target.ClientSessionStarted( clientSessionId_1, dateTime_1 );

            Guid clientSessionId_2 = new Guid( "468CF8B1-4B26-4BF7-B9B1-DA57249F9281" );
            DateTime dateTime_2 = DateTime.Now;
            target.ClientSessionStarted( clientSessionId_2, dateTime_2 );

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                clientSession clientSession_1 = z4sEntities.clientSession.Where( cs1 => cs1.clientSession_id == clientSessionId_1 ).SingleOrDefault();
                Assert.IsNotNull( clientSession_1.clientSession_startDateTimeOnServer );

                clientSession clientSession_2 = z4sEntities.clientSession.Where( cs2 => cs2.clientSession_id == clientSessionId_2 ).SingleOrDefault();
                Assert.IsNotNull( clientSession_2.clientSession_startDateTimeOnServer );
            }
        }

        /// <summary>
        ///Test pour ClientSessionEndedSuccessfully
        ///</summary>
        [TestMethod()]
        public void ClientSessionEndedSuccessfullyTest()
        {
            ILogger target = CreateILogger();
            Guid clientSessionId = new Guid( "87843D09-C873-45DA-8E0E-B66A30D719A6" );
            DateTime dateTime = DateTime.Now;
            target.ClientSessionEndedSuccessfully( clientSessionId, dateTime );

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                clientSession clientSession = z4sEntities.clientSession.Where( cs => cs.clientSession_id == clientSessionId ).SingleOrDefault();
                Assert.IsNotNull( clientSession.clientSession_endDateTimeOnServer );
            }
        }

        /// <summary>
        ///Test pour ClientSessionEndedWithError
        ///</summary>
        [TestMethod()]
        public void ClientSessionEndedWithErrorTest()
        {
            ILogger target = CreateILogger();
            Guid clientSessionId = new Guid( "468CF8B1-4B26-4BF7-B9B1-DA57249F9281" );
            DateTime dateTime = DateTime.Now;
            string exceptionMessage = "Exception Message clientSessionName_2";
            target.ClientSessionEndedWithError( clientSessionId, dateTime, exceptionMessage );

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                clientSession clientSession = z4sEntities.clientSession.Where( cs => cs.clientSession_id == clientSessionId ).SingleOrDefault();
                Assert.IsNotNull( clientSession.clientSession_error );
            }
        }

        /// <summary>
        ///Test pour ActivityStarted
        ///</summary>
        [TestMethod()]
        public void ActivityStartedTest()
        {
            ILogger target = CreateILogger();

            // ClientSession 1 - mefExecution
            Guid clientSessionId_1 = new Guid( "87843D09-C873-45DA-8E0E-B66A30D719A6" );
            Guid activityId_1_mef = new Guid( "B09D041B-67EF-4B1F-83E0-68C3BA4559C3" );
            DateTime dateTime_1_mef = DateTime.Now;
            target.ActivityStarted( clientSessionId_1, activityId_1_mef, dateTime_1_mef );

            // ClientSession 1 - sqlceExecution
            Guid activityId_1_sqlceExecution = new Guid( "1399B199-45DE-4DF3-8BC4-ED485E759631" );
            DateTime dateTime_1_sqlceExecution = DateTime.Now;
            target.ActivityStarted( clientSessionId_1, activityId_1_sqlceExecution, dateTime_1_sqlceExecution );

            // ClientSession 1 - sqlServerExecution
            Guid activityId_1_sqlServerExecution = new Guid( "973DF053-E2EB-411C-A934-CC3A21B33A03" );
            DateTime dateTime_1_sqlServerExecution = DateTime.Now;
            target.ActivityStarted( clientSessionId_1, activityId_1_sqlServerExecution, dateTime_1_sqlServerExecution );

            // ClientSession 1 - sqlceBackup
            Guid activityId_1_sqlceBackup = new Guid( "B5528FBA-9D65-4331-83C1-0A541507A2D8" );
            DateTime dateTime_1_sqlceBackup = DateTime.Now;
            target.ActivityStarted( clientSessionId_1, activityId_1_sqlceBackup, dateTime_1_sqlceBackup );

            // ClientSession 2
            Guid clientSessionId_2 = new Guid( "468CF8B1-4B26-4BF7-B9B1-DA57249F9281" );
            Guid activityId_2 = new Guid( "1D0C410E-0862-42C1-975E-650A214716DC" );
            DateTime dateTime_2 = DateTime.Now;
            target.ActivityStarted( clientSessionId_2, activityId_2, dateTime_2 );

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                activityHistory activityHistory_1_mef = z4sEntities.activityHistory.Where
                    ( ah_1_mef => ah_1_mef.activityHistory_clientSessionId == clientSessionId_1 &&
                                  ah_1_mef.activityHistory_activityId == activityId_1_mef ).SingleOrDefault();
                Assert.IsNotNull( activityHistory_1_mef );

                activityHistory activityHistory_1_sqlceExecution = z4sEntities.activityHistory.Where
                    ( ah_1_sqlceExecution => ah_1_sqlceExecution.activityHistory_clientSessionId == clientSessionId_1 &&
                                             ah_1_sqlceExecution.activityHistory_activityId == activityId_1_sqlceExecution ).SingleOrDefault();
                Assert.IsNotNull( activityHistory_1_sqlceExecution );

                activityHistory activityHistory_1_sqlServerExecution = z4sEntities.activityHistory.Where
                    ( ah_1_sqlServerExecution => ah_1_sqlServerExecution.activityHistory_clientSessionId == clientSessionId_1 &&
                                                 ah_1_sqlServerExecution.activityHistory_activityId == activityId_1_sqlServerExecution ).SingleOrDefault();
                Assert.IsNotNull( activityHistory_1_sqlServerExecution );

                activityHistory activityHistory_1_sqlceBackup = z4sEntities.activityHistory.Where
                    ( ah_1_sqlceBackup => ah_1_sqlceBackup.activityHistory_clientSessionId == clientSessionId_1 &&
                                                 ah_1_sqlceBackup.activityHistory_activityId == activityId_1_sqlceBackup ).SingleOrDefault();
                Assert.IsNotNull( activityId_1_sqlceBackup );

                activityHistory activityHistory_2 = z4sEntities.activityHistory.Where
                    ( ah_2 => ah_2.activityHistory_clientSessionId == clientSessionId_2 &&
                                                 ah_2.activityHistory_activityId == activityId_2 ).SingleOrDefault();
                Assert.IsNotNull( activityHistory_2 ); ;
            }
        }

        /// <summary>
        ///Test pour ActivityEndedSuccessfully
        ///</summary>
        [TestMethod()]
        public void ActivityEndedSuccessfullyTest()
        {
            ILogger target = CreateILogger();
            Guid clientSessionId = new Guid( "87843D09-C873-45DA-8E0E-B66A30D719A6" );
            Guid activityId = new Guid( "B09D041B-67EF-4B1F-83E0-68C3BA4559C3" );
            DateTime dateTime = DateTime.Now;
            target.ActivityEndedSuccessfully( clientSessionId, activityId, dateTime );

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                activityHistory activityHistory = z4sEntities.activityHistory.Where
                    ( ah => ah.activityHistory_clientSessionId == clientSessionId &&
                                  ah.activityHistory_activityId == activityId ).SingleOrDefault();
                Assert.IsNotNull( activityHistory.activityHistory_endDateTimeOnServer );
            }
        }

        /// <summary>
        ///Test pour ActivityEndedWithError
        ///</summary>
        [TestMethod()]
        public void ActivityEndedWithErrorTest()
        {
            ILogger target = CreateILogger();
            Guid clientSessionId = new Guid( "468CF8B1-4B26-4BF7-B9B1-DA57249F9281" );
            Guid activityId = new Guid( "1D0C410E-0862-42C1-975E-650A214716DC" );
            DateTime dateTime = DateTime.Now;
            string exceptionMessage = "Exception Message Activity clientSessionName_2";
            target.ActivityEndedWithError( clientSessionId, activityId, dateTime, exceptionMessage );

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                activityHistory activityHistory = z4sEntities.activityHistory.Where
                    ( ah => ah.activityHistory_clientSessionId == clientSessionId &&
                                  ah.activityHistory_activityId == activityId ).SingleOrDefault();
                Assert.IsNotNull( activityHistory.activityHistory_error );
            }
        }

        /// <summary>
        ///Test pour LogMefExecution
        ///</summary>
        [TestMethod()]
        public void LogMefExecutionTest()
        {
            ILogger target = CreateILogger();
            Guid clientSessionId = new Guid( "87843D09-C873-45DA-8E0E-B66A30D719A6" );
            Guid activityId = new Guid( "B09D041B-67EF-4B1F-83E0-68C3BA4559C3" );
            string metadata = string.Empty;
            target.LogMefExecution( clientSessionId, activityId, metadata );

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                activityHistory activityHistory = z4sEntities.activityHistory.Where
                    ( ah => ah.activityHistory_clientSessionId == clientSessionId &&
                                  ah.activityHistory_activityId == activityId ).SingleOrDefault();
                mefExecutionHistory mefExecutionHistory = z4sEntities.mefExecutionHistory.Where
                    ( mef => mef.mefExecutionHistory_activityHistoryId == activityHistory.activityHistory_id ).SingleOrDefault();
                Assert.IsNotNull( mefExecutionHistory );
            }
        }

        /// <summary>
        ///Test pour LogSqlCEexecution
        ///</summary>
        [TestMethod()]
        public void LogSqlCEexecutionTest()
        {
            ILogger target = CreateILogger();
            Guid clientSessionId = new Guid( "87843D09-C873-45DA-8E0E-B66A30D719A6" );
            Guid activityId = new Guid( "1399B199-45DE-4DF3-8BC4-ED485E759631" );
            sqlceExecutionContext sqlceExecutionContext = null;
            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                sqlceExecutionContext = z4sEntities.sqlceExecutionContext.Where( executionContext => executionContext.sqlceExecutionContext_id == new Guid( "F11420F0-FDF8-458F-AFB2-D836F03D1AC5" ) ).SingleOrDefault();
                target.LogSqlCEexecution( clientSessionId, activityId, sqlceExecutionContext );
            }

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                activityHistory activityHistory = z4sEntities.activityHistory.Where
                    ( ah => ah.activityHistory_clientSessionId == clientSessionId &&
                                  ah.activityHistory_activityId == activityId ).SingleOrDefault();
                sqlceExecutionHistory sqlceExecutionHistory = z4sEntities.sqlceExecutionHistory.Where
                    ( sqlce => sqlce.sqlceExecutionHistory_activityHistoryId == activityHistory.activityHistory_id ).SingleOrDefault();
                Assert.IsNotNull( sqlceExecutionHistory );
            }
        }

        /// <summary>
        ///Test pour LogSQLServerExecution
        ///</summary>
        [TestMethod()]
        public void LogSQLServerExecutionTest()
        {
            ILogger target = CreateILogger();
            Guid clientSessionId = new Guid( "87843D09-C873-45DA-8E0E-B66A30D719A6" );
            Guid activityId = new Guid( "973DF053-E2EB-411C-A934-CC3A21B33A03" );
            sqlServerExecutionContext sqlServerExecutionContext = null;
            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                sqlServerExecutionContext = z4sEntities.sqlServerExecutionContext.Where( executionContext => executionContext.sqlServerExecutionContext_id == new Guid( "6ADA4C66-31EA-45AF-A404-1AF76ECE4D01" ) ).SingleOrDefault();
                target.LogSQLServerExecution( clientSessionId, activityId, sqlServerExecutionContext );
            }

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                activityHistory activityHistory = z4sEntities.activityHistory.Where
                    ( ah => ah.activityHistory_clientSessionId == clientSessionId &&
                                  ah.activityHistory_activityId == activityId ).SingleOrDefault();
                sqlServerExecutionHistory sqlServerExecutionHistory = z4sEntities.sqlServerExecutionHistory.Where
                    ( sqlServer => sqlServer.sqlServerExecutionHistory_activityHistoryId == activityHistory.activityHistory_id ).SingleOrDefault();
                Assert.IsNotNull( sqlServerExecutionHistory );
            }
        }

        /// <summary>
        ///Test pour LogBackupActivity
        ///</summary>
        [TestMethod()]
        public void LogBackupActivityTest()
        {
            ILogger target = CreateILogger();
            Guid clientSessionId = new Guid( "87843D09-C873-45DA-8E0E-B66A30D719A6" );
            Guid activityId = new Guid( "B5528FBA-9D65-4331-83C1-0A541507A2D8" );
            byte[] sqlCeDatabase = new byte[ 0 ];
            target.LogBackupActivity( clientSessionId, activityId, sqlCeDatabase );

            using ( Zen4SyncRepositoryEntities z4sEntities = new Zen4SyncRepositoryEntities() )
            {
                activityHistory activityHistory = z4sEntities.activityHistory.Where
                    ( ah => ah.activityHistory_clientSessionId == clientSessionId &&
                                  ah.activityHistory_activityId == activityId ).SingleOrDefault();
                sqlceBackupHistory sqlceBackupHistory = z4sEntities.sqlceBackupHistory.Where
                    ( sqlce => sqlce.sqlceBackupHistory_activityHistoryId == activityHistory.activityHistory_id ).SingleOrDefault();
                Assert.IsNotNull( sqlceBackupHistory );
            }
        }

    }
}
